package no.adhocit.mvc;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.logicsector.soapclient.SoapClient;

/**
 * Handles requests for the application home page.
 */
@Controller
public class GrunnstoffController {
	
	private static final Logger logger = LoggerFactory.getLogger(GrunnstoffController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	/*@RequestMapping(value = "/grunnstoff", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		SoapClient soapResult = new SoapClient();
		
		String grunnstoffString = soapResult.hentGrunnstoff("Helium");
		
		model.addAttribute("grunnstoff", grunnstoffString );

		
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "grunnstoff";
	}*/
	
	
	 @RequestMapping("/grunnstoff")
	   public ModelAndView finnGrunnstoff(@ModelAttribute("SpringWeb")Grunnstoff grunnstoff, Model model) {
		 return new ModelAndView("grunnstoff", "command", new Grunnstoff());
	   }

	 @RequestMapping(value = "/finnGrunnstoff", method = RequestMethod.POST)
	    public String addContact(@ModelAttribute("grunnstoff")Grunnstoff grunnstoff, BindingResult result, Model model) {
	         
	        System.out.println("First Name:" + grunnstoff.getNavn());
	        
	        SoapClient soapResult = new SoapClient();
			
			String grunnstoffString = soapResult.hentGrunnstoff(grunnstoff.getNavn());
			
			model.addAttribute("resultat", grunnstoffString );
	        
	         
	        return "finnGrunnstoff";
	    }

	 
	
	
	
}
